package org.example.web.service;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.example.account.api.feign.FeignDemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Optional;

/**
 * @author Administrator
 */
@Service
public class AccountService {

	private RestTemplate restTemplate;

	private FeignDemo feignDemo;

	public AccountService(RestTemplate restTemplate, FeignDemo feignDemo) {
		this.restTemplate = restTemplate;
		this.feignDemo = feignDemo;
	}

	/*
	 * @HystrixCommand(fallbackMethod = "getFallbackName", commandProperties =
	 * { @HystrixProperty( name = "execution.isolation.thread.timeoutInMilliseconds",
	 * value = "1000") })
	 */
	public String getDataFromSpringCloudK8SProvider() {

		/*
		 * Optional<RestTemplate> restTemplateOptional =
		 * Optional.ofNullable(restTemplate); if (!restTemplateOptional.isPresent()) {
		 * return "restTemple is null"; }
		 *
		 * return this.restTemplate.getForObject("http://account-service/name",
		 * String.class);
		 */
		return feignDemo.getName();
	}

	/**
	 * 熔断时调用的方法
	 * @return
	 */
	private String getFallbackName() {
		return "Fallback" + ", "
				+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
	}

}
